import { http } from "@/utils/http";
// import { getToken } from "@/utils/auth";
import { getToken175, getToken43 } from "@/utils/token";

type Result = {
  message: string;
  data: any;
  code: number;
};

// 为task API添加Authorization头的辅助函数
const taskRequest = <T>(method: string, url: string, data?: object) => {
  const token = getToken175(); // 每次调用都获取最新的token
  return http.request<T>(method as any, url, {
    data,
    headers: {
      Authorization: token ? `Bearer ${token}` : undefined,
    },
  });
};

const remoteRequest = <T>(method: string, url: string, data?: object) => {
  const remoteToken = getToken43(); // 每次调用都获取最新的token
  return http.request<T>(method as any, url, {
    data,
    headers: {
      Authorization: remoteToken ? `Bearer ${remoteToken}` : undefined,
    },
  });
};

/** 获取资产列表 */
export const getAssetList = ({
  data,
  type = "asset",
}: {
  data: object;
  type: string;
}) => {
  return taskRequest<Result>("post", `/api/api/${type}/data`, data);
};

// ============ 备案域名管理模块 API ============

/** 获取配置列表 */
export const getConfigList = (data?: {
  category?: string;
  hide_sensitive?: boolean;
}) => {
  return taskRequest<Result>("post", "/api/config/list", data);
};

/** 获取配置分类 */
export const getConfigCategories = () => {
  return taskRequest<Result>("post", "/api/config/categories");
};

/** 更新配置 */
export const updateConfig = (data?: {
  config_key: string;
  config_value: string;
}) => {
  return taskRequest<Result>("post", "/api/config/update", data);
};

/** 测试配置连接 */
export const testConfig = (data?: { config_type: string }) => {
  return taskRequest<Result>("post", "/api/config/test", data);
};

/** 重载配置缓存 */
export const reloadConfig = () => {
  return taskRequest<Result>("post", "/api/config/reload");
};

/** 搜索备案信息 */
export const searchBeian = (data?: {
  type: string; // web/app/mapp
  keyword: string;
  page?: number;
  size?: number;
}) => {
  return taskRequest<Result>("post", "/api/beian/search", data);
};

/** 保存备案域名 */
export const saveBeian = (data?: { type: string; records: any[] }) => {
  return taskRequest<Result>("post", "/api/beian/save", data);
};

/** 获取备案列表 */
export const getBeianList = (data?: {
  type?: string;
  keyword?: string;
  page?: number;
  size?: number;
}) => {
  return taskRequest<Result>("post", "/api/beian/list", data);
};

/** 获取备案详情 */
export const getBeianDetail = (data?: { id: number }) => {
  return taskRequest<Result>("post", "/api/beian/detail", data);
};

/** 删除备案域名 */
export const deleteBeian = (data?: { id?: number; ids?: number[] }) => {
  return taskRequest<Result>("post", "/api/beian/delete", data);
};

/** 子域名扫描 */
export const scanSubdomain = (data?: {
  beian_id: number;
  domain?: string;
  auto_save?: boolean;
}) => {
  return taskRequest<Result>("post", "/api/subdomain/scan", data);
};

/** 手动添加子域名 */
export const addSubdomain = (data?: {
  beian_id: number;
  subdomain: string;
  ip_address: string;
  port?: string;
}) => {
  return taskRequest<Result>("post", "/api/subdomain/add", data);
};

/** 获取子域名列表 */
export const getSubdomainList = (data?: {
  beian_id?: number;
  keyword?: string;
  page?: number;
  size?: number;
  order_by?: string;
}) => {
  return taskRequest<Result>("post", "/api/subdomain/list", data);
};

/** 删除子域名 */
export const deleteSubdomain = (data?: { id?: number; ids?: number[] }) => {
  return taskRequest<Result>("post", "/api/subdomain/delete", data);
};

/** 删除资产 */
export const deleteAsset = (data?: object) => {
  return taskRequest<Result>("post", "/api/api/data/delete", data);
};

/** 添加标签 */
export const addTag = (data?: object) => {
  return taskRequest<Result>("post", "/api/api/data/add/tag", data);
};

/** 删除标签 */
export const deleteTag = (data?: object) => {
  return taskRequest<Result>("post", "/api/api/data/delete/tag", data);
};

/** 更新资产状态 */
export const updateAssetStatus = (data?: object) => {
  return taskRequest<Result>("post", "/api/api/data/update/status", data);
};
